var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/work-flow/WFDynamicBranchWin.tsx
var WFDynamicBranchWin_exports = {};
__export(WFDynamicBranchWin_exports, {
  WFDynamicBranchWin: () => WFDynamicBranchWin
});
module.exports = __toCommonJS(WFDynamicBranchWin_exports);
var import_react = require("react");
var import_functionalComponent = require("../../functionalComponent");
var import_util = require("../../util");
var import_util2 = require("./util");
function WFDynamicBranchWin(props) {
  const { branchData = [], callBack, cancelBack } = props;
  const mCtx = (0, import_react.useContext)(import_functionalComponent.ModalContext);
  const [checkedKeys, setCheckedKeys] = (0, import_react.useState)({ checked: [] });
  const memoTree = (0, import_react.useMemo)(() => {
    const rootNode = {
      title: import_util2.NGLang.curNodeName,
      key: "root",
      canAssignMultiFlows: false
    };
    const keyRefNode = { root: rootNode };
    const getTreeNode = (nodes, parentNode) => {
      return nodes.map((node) => {
        const nodeData = {
          ...node,
          key: node.id,
          title: node.name,
          // disabled: !node.needAssignFlow && node.children.length !== 0,
          parentNode
        };
        keyRefNode[nodeData.key] = nodeData;
        if (node.children && node.children.length) {
          nodeData.children = getTreeNode(node.children, nodeData);
        } else {
          nodeData.children = [];
        }
        return nodeData;
      });
    };
    rootNode.children = getTreeNode(branchData, rootNode);
    return { root: rootNode, treeData: rootNode.children, keyRefNode };
  }, [branchData]);
  const getCheckedNodes = (0, import_util.useRefCallback)((node) => {
    if (node) {
      return node.children.filter((itemNode) => {
        return !itemNode.needAssignFlow || checkedKeys.checked.includes(itemNode.key);
      });
    }
    return checkedKeys.checked.map((key) => memoTree.keyRefNode[key]);
  });
  const isValid = (0, import_util.useRefCallback)((node) => {
    if (node.children.length < 1) {
      return true;
    }
    const checkedSubNodes = getCheckedNodes(node);
    if (checkedSubNodes.length < 1) {
      (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, (0, import_util2.stringFormat)(import_util2.NGLang.nodeMustHasChild, node.name)).then();
      return false;
    }
    if (node.canAssignMultiFlows && checkedSubNodes.length > 1) {
      (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, (0, import_util2.stringFormat)(import_util2.NGLang.nodeMustOnlyOneChild, node.name)).then();
      return false;
    }
    return !checkedSubNodes.some((itemNode) => !isValid(itemNode));
  });
  const cascadeNode = (0, import_util.useRefCallback)((node, checkedSet, checked) => {
    (0, import_util2.cascadeBy)(node, (itemNode) => {
      if (!itemNode.canAssignMultiFlows) {
        checked ? checkedSet.add(itemNode.key) : checkedSet.delete(itemNode.key);
      }
    });
  });
  const checkBrother = (0, import_util.useRefCallback)((checkedSet, checkedNode, checked) => {
    checkedNode.parentNode.children.forEach((nd) => {
      if (nd.key !== checkedNode.key) {
        cascadeNode(nd, checkedSet, checked);
      }
    });
  });
  const checkParent = (0, import_util.useRefCallback)((checkedSet, currentNode, checked) => {
    if (currentNode.key === "root") {
      return;
    }
    if (!currentNode.canAssignMultiFlows) {
      checked ? checkedSet.add(currentNode.key) : checkedSet.delete(currentNode.key);
    }
    if (checked) {
      if (currentNode.parentNode.key !== "root") {
        if (!currentNode.parentNode.canAssignMultiFlows) {
          checkBrother(checkedSet, currentNode, false);
        }
        checkParent(checkedSet, currentNode.parentNode, true);
      }
    }
  });
  const onCheckHandler = (0, import_util.useRefCallback)((newCheckedKeys, { checked, node }) => {
    const checkedSet = new Set(newCheckedKeys.checked);
    if (checked) {
      if (node.parentNode.key !== "root") {
        if (!node.parentNode.canAssignMultiFlows) {
          checkBrother(checkedSet, node, false);
        }
        checkParent(checkedSet, node.parentNode, true);
      }
    } else {
      cascadeNode(node, checkedSet, false);
    }
    setCheckedKeys({ checked: [...checkedSet] });
  });
  (0, import_react.useEffect)(() => {
    mCtx.ins.setApi({
      invokeOkHandler: async () => {
        var _a;
        if (!isValid(memoTree.root)) {
          return;
        }
        const checkedNodes = getCheckedNodes();
        await (callBack == null ? void 0 : callBack(
          checkedNodes.map((checkedNode) => {
            return {
              nodeId: checkedNode.parentId,
              nodeName: checkedNode.name,
              targetNodeId: checkedNode.id
            };
          })
        ));
        (_a = mCtx.ins) == null ? void 0 : _a.destroy();
      },
      invokeCancelHandler: async () => {
        var _a;
        await (cancelBack == null ? void 0 : cancelBack());
        (_a = mCtx.ins) == null ? void 0 : _a.destroy();
      }
    });
  }, [memoTree]);
  return /* @__PURE__ */ React.createElement(import_util.Layout, { style: { height: 320, padding: "5px 12px" } }, /* @__PURE__ */ React.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ React.createElement(
    import_functionalComponent.Tree,
    {
      checkable: true,
      checkStrictly: true,
      checkedKeys,
      defaultExpandAll: true,
      onCheck: onCheckHandler,
      selectable: false,
      treeData: memoTree.treeData
    }
  )));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  WFDynamicBranchWin
});
